<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>单元管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
    <link rel="stylesheet" href="/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>楼 栋：</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">单元</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn" lay-submit lay-filter="data-search-btn"><i
                                    class="layui-icon"></i> 搜 索
                            </button>
                            <button type="submit" class="layui-btn" lay-submit lay-filter="data-add-btn"><i
                                    class="fa fa-plus"></i> 添 加
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container"></div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>

<!-- 添加表单弹窗 -->
<script type="text/html" id="modelAdd">
    <form id="modelAddForms" lay-filter="modelAddForm" class="layui-form model-form">
        <div class="layui-form-item">
            <label class="layui-form-label">单元名称</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入单元名" type="text" class="layui-input" maxlength="20"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">隶属单元</label>
            <div class="layui-input-block">
                <select name="buildingId" lay-verType="tips" lay-verify="required" required>
                    <option value="">请选择单元</option>
                    <option value="1">A</option>
                    <option value="2">B</option>
                    <option value="3">C</option>
                    <option value="4">B</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-primary" type="reset" id="closeBtn">重置</button>
                <button class="layui-btn" lay-filter="modelSubmitAdd" lay-submit>保存</button>
            </div>
        </div>
    </form>
</script>

<!-- 修改表单弹窗 -->
<script type="text/html" id="modelEdit">
    <form id="modelEditForms" lay-filter="modelEditForm" class="layui-form model-form">
        <input type="hidden" name="id" id="id">
        <div class="layui-form-item">
            <label class="layui-form-label">单元名称</label>
            <div class="layui-input-block">
                <input name="name" id="name" placeholder="请输入单元名" type="text" class="layui-input" maxlength="20"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">隶属单元</label>
            <div class="layui-input-block">
                <select name="buildingId" id="buildingId" lay-verType="tips" lay-verify="required" required>
                    <option value="1">A</option>
                    <option value="2">B</option>
                    <option value="3">C</option>
                    <option value="4">B</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-primary" type="reset">重置</button>
                <button class="layui-btn" lay-filter="modelSubmitEdit" lay-submit>保存</button>
            </div>
        </div>
    </form>
</script>

<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        // 表格渲染
        table.render({
            elem: '#currentTableId',
            url: '/DanyuanServlet?opt=list',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {title: 'ID', type: 'numbers'},
                {field: 'name',width:700,title: '单元名',sort: true, align: "center"},
                {
                    field: 'buildingId', title: '隶属楼栋', sort: true, templet: function (row) {
                        if (row.buildingId == 1) {
                            return '<p>A栋</p>'
                        } else if(row.buildingId == 2){
                            return '<p>B栋</p>'
                        }else if(row.buildingId == 3){
                            return '<p>C栋</p>'
                        }else if(row.buildingId == 4){
                            return '<p>D栋</p>'
                        }
                    }
                },
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [2, 4, 6, 8, 10],
            limit: 4,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {  // data 表单数据对象 name键 value值
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    name: data.field.name
                }
            }, 'data');
            return false;
        });

        // 监听添加操作
        form.on('submit(data-add-btn)', function (data) {  // data 表单数据对象 name键 value值
            // 打开添加对话框
            showAddModel();
            return false;
        });

        // 显示添加表单弹窗
        function showAddModel() {
            // 打开弹窗
            layer.open({
                type: 1,
                title: '添加单元',
                content: $('#modelAdd').html(),
                success: function (layero, dIndex) {
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    // 表单提交事件
                    form.render();
                    form.on('submit(modelSubmitAdd)', function (data) {  // 表单数据对象
                        layer.load(2);
                        $.ajax({
                            url: '/DanyuanServlet?opt=add',
                            type: "POST",
                            data: data.field,
                            dataType: "json",
                            success: function (data) {
                                layer.closeAll('loading');
                                if (data.code == 0) {
                                    layer.close(dIndex);
                                    layer.msg(data.msg, {icon: 1});
                                    // 刷新表格
                                    //执行搜索重载
                                    table.reload('currentTableId')
                                } else {
                                    layer.msg(data.msg, {icon: 2});
                                }
                            }
                        })
                        return false;
                    });
                }
            });
        }

        // 监听修改删除
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                showEditModel(data);
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm(`确定要删除单元\${data.name}吗?`, function (index) {
                    $.getJSON('/DanyuanServlet?opt=delete', `id=\${data.id}`, function (data) {
                        if (data.code == 0) {
                            layer.close(index);
                            layer.msg(data.msg, {icon: 1});
                            // 刷新数据
                            table.reload('currentTableId')
                        } else {
                            layer.msg(data.msg, {icon: 2});
                        }
                    })

                });
            }
        });

        // 显示修改表单弹窗
        function showEditModel(data) {
            // 打开弹窗
            layer.open({
                type: 1,
                title: '修改单元',
                content: $('#modelEdit').html(),
                success: function (layero, dIndex) {
                    // 绑定数据 | ajax request
                    $('#id').val(data.id)
                    $('#name').val(data.name)
                    $('#type').val(data.type)
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    // 表单提交事件
                    form.render();
                    form.on('submit(modelSubmitEdit)', function (data) {  // 表单数据对象
                        layer.load(2);
                        $.ajax({
                            url: '/DanyuanServlet?opt=edit',
                            type: "POST",
                            data: data.field,
                            dataType: "json",
                            success: function (data) {
                                layer.closeAll('loading');
                                if (data.code == 0) {
                                    layer.close(dIndex);
                                    layer.msg(data.msg, {icon: 1});
                                    // 刷新表格
                                    //执行搜索重载
                                    table.reload('currentTableId')
                                } else {
                                    layer.msg(data.msg, {icon: 2});
                                }
                            }
                        })
                        return false;
                    });
                }
            });
        }

    });
</script>

</body>
</html>